package com.atguigu.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msm.service.MsmService;
import com.atguigu.msm.utils.RandomUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    // 发送短信的方法
    @Override
    public boolean sendMessage(Map<String,Object> param,String phone) {


        // 发送短信的方法
        DefaultProfile profile =
                DefaultProfile.getProfile("defalut","LTAI4G8FvZNTb5mHjbvsFNuw","1IFxkGJgcHqrqLlnBJq1TlderGgKTo");
        // 创建最终用于发送的对象
        IAcsClient client = new DefaultAcsClient(profile);
        // 设置相关参数 固定的 不能修改
        CommonRequest request = new CommonRequest();
        //request.setSysProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        // 设置发送的相关参数
        request.putQueryParameter("PhoneNumbers",phone); //设置手机号
        request.putQueryParameter("SignName","我的佳家学习在线教育网站"); //申请的阿里云的签名名称
        request.putQueryParameter("TemplateCode","SMS_205440806"); //申请的阿里云的模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码的设置 json格式


        try {
            // 最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
